package com.universal.schedule;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.universal.schedule.adapter.TabFragmentPagerAdapter;
import com.universal.schedule.db.DBManager;
import com.universal.schedule.fragments.AlarmsFragments;
import com.universal.schedule.fragments.CalendarFragments;
import com.universal.schedule.fragments.DiaryFragments;
import com.universal.schedule.fragments.ScheduleFragments;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity implements OnClickListener {

	private long exitTime = 0;

	private ViewPager mViewPager;
	private ImageButton mTabMain;
	private TextView mTvMain;
	private ImageButton mTabConfirm;
	private TextView mTvConfirm;
	private ImageButton mTabUpdate;
	private TextView mTvUpdate;
	private ImageButton mTabReport;
	private TextView mTvReport;
	private List<Fragment> mFragments;
	private TabFragmentPagerAdapter mAdapter;
	private LinearLayout mLLMain;
	private LinearLayout mLLConfirm;
	private RelativeLayout mLLUpdate;
	private RelativeLayout mLLReport;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		findview();
		setSelect(0);
		mViewPager.setCurrentItem(0);
		initClickListener();
		DBManager.getInstance(this)  .getDatabaseHelper();
	}

	@SuppressWarnings("deprecation")
	private void findview() {
		mViewPager = (ViewPager) findViewById(R.id.tab_main_viewpager);
		mTabMain = (ImageButton) findViewById(R.id.tab_main);
		mTvMain = (TextView) findViewById(R.id.text_main);
		mTabConfirm = (ImageButton) findViewById(R.id.tab_confirm);
		mTvConfirm = (TextView) findViewById(R.id.text_confirm);
		mTabUpdate = (ImageButton) findViewById(R.id.tab_update);
		mTvUpdate = (TextView) findViewById(R.id.text_update);
		mTabReport = (ImageButton) findViewById(R.id.tab_report);
		mTvReport = (TextView) findViewById(R.id.text_report);
		
		mLLMain = (LinearLayout) findViewById(R.id.id_tab_main);
		mLLConfirm = (LinearLayout) findViewById(R.id.id_tab_confirm);
		mLLUpdate = (RelativeLayout) findViewById(R.id.id_tab_main_update);
		mLLReport = (RelativeLayout) findViewById(R.id.id_tab_main_report);

		mFragments = new ArrayList<Fragment>();
		Fragment diaryFragments = new DiaryFragments();
		Fragment scheduleFragments = new ScheduleFragments();
		Fragment alarmsFragments = new AlarmsFragments();
		Fragment calendarFragments = new CalendarFragments();
		mFragments.add(diaryFragments);
		mFragments.add(scheduleFragments);
		mFragments.add(alarmsFragments);
		mFragments.add(calendarFragments);

		mAdapter = new TabFragmentPagerAdapter(getSupportFragmentManager(), mFragments);
		mViewPager.setAdapter(mAdapter);
		mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
			}

			@Override
			public void onPageSelected(int position) {
				int currentItem = mViewPager.getCurrentItem();
				initTabImage();
				switch (currentItem) {
				case 0:
					mTabMain.setImageResource(R.drawable.query_color);
					mTvMain.setTextColor(MainActivity.this.getResources().getColor(R.color.press));
					mTvConfirm.setTextColor(MainActivity.this.getResources().getColor(R.color.normal));
					mTvUpdate.setTextColor(MainActivity.this.getResources().getColor(R.color.normal));
					mTvReport.setTextColor(MainActivity.this.getResources().getColor(R.color.normal));

					break;
				case 1:
					mTabConfirm.setImageResource(R.drawable.bang_color);
					mTvConfirm.setTextColor(MainActivity.this.getResources().getColor(R.color.press));
					mTvUpdate.setTextColor(MainActivity.this.getResources().getColor(R.color.normal));
					mTvReport.setTextColor(MainActivity.this.getResources().getColor(R.color.normal));
					mTvMain.setTextColor(MainActivity.this.getResources().getColor(R.color.normal));
					break;
				case 2:
					mTabUpdate.setImageResource(R.drawable.update_color);
					mTvUpdate.setTextColor(MainActivity.this.getResources().getColor(R.color.press));
					mTvConfirm.setTextColor(MainActivity.this.getResources().getColor(R.color.normal));
					mTvReport.setTextColor(MainActivity.this.getResources().getColor(R.color.normal));
					mTvMain.setTextColor(MainActivity.this.getResources().getColor(R.color.normal));
					break;
				case 3:
					mTabReport.setImageResource(R.drawable.report_color);
					mTvReport.setTextColor(MainActivity.this.getResources().getColor(R.color.press));
					mTvUpdate.setTextColor(MainActivity.this.getResources().getColor(R.color.normal));
					mTvConfirm.setTextColor(MainActivity.this.getResources().getColor(R.color.normal));
					mTvMain.setTextColor(MainActivity.this.getResources().getColor(R.color.normal));
					break;
				}
			}

			@Override
			public void onPageScrollStateChanged(int state) {

			}
		});

	}

	private void setSelect(int i) {

		initTabImage();
		switch (i) {
		case 0:
			mTabMain.setImageResource(R.drawable.query_color);
			mTvMain.setTextColor(this.getResources().getColor(R.color.press));
			mTvConfirm.setTextColor(this.getResources().getColor(R.color.normal));
			mTvUpdate.setTextColor(this.getResources().getColor(R.color.normal));
			mTvReport.setTextColor(this.getResources().getColor(R.color.normal));
			break;
		case 1:
			mTabConfirm.setImageResource(R.drawable.bang_color);
			mTvConfirm.setTextColor(this.getResources().getColor(R.color.press));
			mTvUpdate.setTextColor(this.getResources().getColor(R.color.normal));
			mTvReport.setTextColor(this.getResources().getColor(R.color.normal));
			mTvMain.setTextColor(this.getResources().getColor(R.color.normal));
			break;
		case 2:
			mTabUpdate.setImageResource(R.drawable.update_color);
			mTvUpdate.setTextColor(this.getResources().getColor(R.color.press));
			mTvConfirm.setTextColor(this.getResources().getColor(R.color.normal));
			mTvReport.setTextColor(this.getResources().getColor(R.color.normal));
			mTvMain.setTextColor(this.getResources().getColor(R.color.normal));
			break;
		case 3:
			mTabReport.setImageResource(R.drawable.report_color);
			mTvReport.setTextColor(this.getResources().getColor(R.color.press));
			mTvUpdate.setTextColor(this.getResources().getColor(R.color.normal));
			mTvConfirm.setTextColor(this.getResources().getColor(R.color.normal));
			mTvMain.setTextColor(this.getResources().getColor(R.color.normal));
			break;
		default:
			break;
		}
		mViewPager.setCurrentItem(i);
	}

	private void initTabImage() {
		mTabMain.setImageResource(R.drawable.query);
		mTabConfirm.setImageResource(R.drawable.bang);
		mTabUpdate.setImageResource(R.drawable.update);
		mTabReport.setImageResource(R.drawable.report);
	}

	private void initClickListener() {
		mLLMain.setOnClickListener(this);
		mLLConfirm.setOnClickListener(this);
		mLLUpdate.setOnClickListener(this);
		mLLReport.setOnClickListener(this);

	}

	@Override
	public void onBackPressed() {
		if (System.currentTimeMillis() - exitTime > 2000) {
			Toast.makeText(MainActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
			exitTime = System.currentTimeMillis();
		} else {
			finish();
			System.exit(0);
			android.os.Process.killProcess(android.os.Process.myPid());
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.id_tab_main:
			setSelect(0);
			break;
		case R.id.id_tab_confirm:
			setSelect(1);
			break;
		case R.id.id_tab_main_update:
			setSelect(2);
			break;
		case R.id.id_tab_main_report:
			setSelect(3);
			break;
		}

	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		 if (resultCode== 1000){
			    //找到AFragment
			    ScheduleFragments fragment= (ScheduleFragments) mFragments.get(1);
			    //调用AFragment的onActivityResult
			    fragment.onActivityResult(requestCode,resultCode,data);
			    }
	}
}
